﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Configuration;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Helpers;
using Vit.Common.Extensions;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Mvc.Results;
using Vit.Web.Helpers;

namespace Vit.Site.Controllers
{
    public class InstallController : Vit.Web.Mvc.Controllers.ControllerBase
    {
        [HttpGet]
        public ActionResult Index()
        {
            return RedirectToAction("Configuration");
        }

        [HttpGet]
        public ActionResult Configuration()
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");
            var adminAccount = config.AppSettings.Settings["AdministratorAccount"].Value;
            IDataProvider<User> provider = Unity.Current.Resolve<IDataProvider<User>>();
            if (!string.IsNullOrWhiteSpace(adminAccount))
            {
                var exists = provider.GetAll().Any(o => o.Account == adminAccount);
                if(exists)
                    return RedirectToAction("DefaultPortal");
            }

            ViewBag.AdministratorAccount = adminAccount;
            User admin = null;
            try
            {
                admin = provider.GetByKeys(adminAccount);
            }
            catch { }
            if (admin == null)
            {
                admin = new User()
                {
                    Account = "admin"
                };
                ViewData["EntityID"] = "";
            }
            else
            {
                ViewData["EntityID"] = admin.Account;
            }
            return View(admin);
        }

        [HttpPost]
        public ActionResult Configuration(User admin, FormCollection form)
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");

            IDataProvider<User> provider = Unity.Current.Resolve<IDataProvider<User>>();
            string pwd = admin.Password;
            admin.Password = SecurityHelper.Hash(pwd);
            if (string.IsNullOrWhiteSpace(form["EntityID"]))
            {
                config.AppSettings.Settings["AdministratorAccount"].Value = admin.Account;
                provider.Insert(admin);
            }
            else
            {
                provider.Modify(admin);
            }
            config.Save();
            Unity.Current.ResolveAny<IMembershipProvider>().ForEach<IMembershipProvider, bool>(o => o.Login(admin.Account, pwd, false));
            return RedirectToAction("DefaultPortal");
        }

        [HttpGet]
        public ActionResult DefaultPortal()
        {
            if (PortalContext.Current.Portal != null && !string.IsNullOrWhiteSpace(PortalContext.Current.Portal.ID))
                return RedirectToAction("DefaultWebSite");

            Portal portal = new Portal()
            {
                ID = "DefaultPortal",
                Caption = "Default Portal",
                Description = "Default Portal"
            };
            return View(portal);
        }

        [HttpPost]
        public ActionResult DefaultPortal(Portal portal)
        {
            IPortalProvider provider = Unity.Current.Resolve<IPortalProvider>();
            provider.AddPortal(portal);
            return RedirectToAction("DefaultWebSite");
        }

        [HttpGet]
        public ActionResult DefaultWebSite()
        {
            if (PortalContext.Current.Site != null && !string.IsNullOrWhiteSpace(PortalContext.Current.Site.ID))
                return RedirectPermanent("/Page");
            
            WebSite site = new WebSite()
            {
                ID = "DefaultSite",
                Caption = "Default Site",
                LogoUrl = "logo.gif",
                ImageUrl = "default_site.jpg",
                FavIcon = "favicon.ico",
                CultureInfo = "zh-CN",
                DefaultCharSet = "utf-8",
                DefaultTitle = "Default Site",
                DefaultMetaKeywords = "Default Site",
                DefaultMetaDescription = "Default Site",
                DomainPattern = ".",
                HomeUrl = "/Home/Index",
                Theme = "Default",
                Skin = "Default",
                TimeZone = 8.0,
                MenuGroup = "default",
                ConfigurationPath = "Default",

                Portal = PortalContext.Current.Portal
            };
            return View(site);
        }

        [HttpPost]
        public ActionResult DefaultWebSite(WebSite website)
        {
            IPortalProvider provider = Unity.Current.Resolve<IPortalProvider>();
            provider.AddSite(PortalContext.Current.Portal, website);
            return RedirectPermanent("/Page");
        }
    }
}
